local skynet = require "skynet"
local rawset = rawset
local setmetatable = setmetatable

---@class Service
local M = {}
function M:new(address)
    local obj = {
        address = address
    }

    -- 不存在的 key 就是要调用的 cmd
    self.__index = function(t, cmd)
        -- self.__index = self
        if self[cmd] then
            return self[cmd]
        end

        local function func(arg1, ...)
            if arg1 == t then
                return skynet.call(t.address, "lua", cmd, ...)
            end

            skynet.send(t.address, "lua", cmd, arg1, ...)
        end

        rawset(t, cmd, func)
        return func
    end

    setmetatable(obj, self)
    return obj
end

function M:_setAddress(addr)
    self.address = addr
end

function M:_call(msg_type, cmd, ...)
    return skynet.call(self.address, msg_type, cmd, ...)
end

function M:_send(msg_type, cmd, ...)
    return skynet.send(self.address, msg_type, cmd, ...)
end

return M